package com.zjy.order.service.impl;

import com.zjy.basic.util.*;
import com.zjy.order.domain.OrderProduct;
import com.zjy.order.domain.OrderProduct;
import com.zjy.order.mapper.OrderProductMapper;
import com.zjy.order.query.OrderProductQuery;
import com.zjy.order.service.OrderProductService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.org.domain.Employee;
import com.zjy.org.domain.Shop;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.org.mapper.ShopMapper;
import com.zjy.pet.domain.Pet;
import com.zjy.pet.domain.SearchMasterMsg;
import com.zjy.pet.domain.SearchMasterMsgAuditLog;
import com.zjy.pet.dto.OrderDto;
import com.zjy.pet.mapper.PetMapper;
import com.zjy.pet.mapper.SearchMasterMsgMapper;
import com.zjy.product.domain.Product;
import com.zjy.user.domain.Logininfo;
import com.zjy.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements OrderProductService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderProduct> queryList(OrderProductQuery query, HttpServletRequest request) {
        //获取当前登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取管理员信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果是店铺管理员
        if (employee.getShop_id()!=null){//店铺管理员：店长 or 店员
            //设置店铺id = 店铺管理员只能查询自己店铺的定单
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.findById(employee.getShop_id());
            //如果店铺管理员id 与 当前登录人id不一样 = 就不是店长
            if (shop.getAdminId()!=employee.getId()){
                //店铺只能查自己店铺  并且  自己的订单。不能查其他店员或店长的订单
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public AjaxResult adopt(Product product, HttpServletRequest request) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setDigest(product.getName());
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setState(0);
        orderProduct.setShopId(27L);
        orderProductMapper.add(orderProduct);
        return new AjaxResult();
    }

    //确认订单
    @Override
    public void confirm(OrderDto orderDto) {
//        // orderId ，支付方式，成交价
//        //1.生成宠物信息
        OrderProduct order = orderProductMapper.findById(orderDto.getId());
//        petMapper.add(pet);
//
//        //2.改订单状态为1，价格为成交价，支付方式
        order.setState(1);
//        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
//        order.setPetId(pet.getId());
        orderProductMapper.update(order);
    }

    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }

    //取消订单:可以单独写接口写业务，页可以直接改【因为前端没有删除功能】
    @Override
    public void del(Long id) {
        OrderProduct order = orderProductMapper.findById(id);
        //改订单的状态：0 1 -1
        order.setState(-1);
        orderProductMapper.update(order);
    }
}
